package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.enums.DeviceEunm;
import com.kaili.basic.enums.DeviceStatusEunm;
import com.kaili.basic.mapper.MdmDeviceCommandMapper;
import com.kaili.basic.pojo.dto.BaseUserAddUserDto;
import com.kaili.basic.pojo.dto.BaseUserUpdateBaseRolesDto;
import com.kaili.basic.pojo.dto.DeviceDTO;
import com.kaili.basic.pojo.entity.*;
import com.kaili.basic.pojo.vo.DeviceVO;
import com.kaili.basic.service.BaseUserService;
import com.kaili.common.pojo.entity.BaseUser;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.tenant.DbContextHolder;
import com.kaili.common.util.CommonConstant;
import com.kaili.common.util.HexStringUtils;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.common.util.SHA256Util;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.kaili.basic.service.IDeviceService;
import com.kaili.basic.mapper.DeviceMapper;

import java.util.*;

/**
*  service实现类
* @author:zhouyijian
* @Time: 2019-07-24
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, DeviceEntity>  implements IDeviceService {

    @Autowired
    BaseUserService baseUserService;
    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private MdmDeviceCommandMapper mdmDeviceCommandMapper;

    @Override
    public RestStatus doCreateDevice(DeviceDTO dto) {

        if(dto==null){
            return CommonStatus.DEVICE_DATA_EMPTY;
        }
        Map<String,Object> map =new HashMap<>();
        map.put("imei",dto.getImei());
        map.put("typeId",dto.getTypeId());
        DeviceVO device =deviceMapper.validateIsExistByImei(map);
        if(device!=null){
            if(device.getModel().equals(dto.getModel())){
                return CommonStatus.DEVICE_MODEL_REPEAT;
            }
            return CommonStatus.DEVICE_REPEAT;
        }
        DeviceEntity entity =setDefaultValue(dto);
        deviceMapper.insert(entity);

/*        MdmDeviceCommand mdmDeviceCommand = new MdmDeviceCommand();
        mdmDeviceCommand.setDeviceId(deviceMapper.getIsActionByImei(dto.getImei()).getId());
        mdmDeviceCommand.setCreateBy(dto.getCreateBy());
        mdmDeviceCommandMapper.addMdmDeviceCommand(mdmDeviceCommand);*/

        return CommonStatus.ADD_OK;
    }


    @Override
    public RestStatus deleteByImei(Long id) {
        Integer employeeId=LoginDetailUtils.getCurrentEmployeeId();
        DeviceEntity entity = new DeviceEntity();
        entity.setId(id);
        entity.setModifyBy(employeeId);
        entity.setModifyTime(new Date());
        entity.setRdStatus(DeviceStatusEunm.DELETE.getCode());
        deviceMapper.updateById(entity);
        return CommonStatus.DELETE_OK;
    }

    @Override
    public RestStatus switchDeviceStatus(String imei) {
        Integer employeeId=LoginDetailUtils.getCurrentEmployeeId();
        DeviceEntity entity =deviceMapper.getIsActionByImei(imei);
        if(entity==null){
            return CommonStatus.DEVICE_DATA_EMPTY;
        }
        if(DeviceStatusEunm.DEVICE_ON.getCode().equals(entity.getIsAction())){
            entity.setIsAction(DeviceStatusEunm.DEVICE__OFF.getCode());
        }else {
            entity.setIsAction(DeviceStatusEunm.DEVICE_ON.getCode());
        }
        entity.setModifyBy(employeeId);
        entity.setModifyTime(new Date());
        deviceMapper.updateById(entity);
        return CommonStatus.UPDATE_OK;
    }


    @Override
    public RestStatus updateDeviceByImei(DeviceDTO dto) {
        Integer employeeId=LoginDetailUtils.getCurrentEmployeeId();
        DeviceEntity entity = new DeviceEntity();
        dto.setModifyBy(employeeId);
        dto.setModifyTime(new Date());
        BeanUtils.copyProperties(dto,entity);
        deviceMapper.updateById(entity);
        return CommonStatus.UPDATE_OK;
    }


    @Override
    public Page<DeviceVO> queryPageList(DeviceDTO dto) {
        Page<DeviceVO>  page= new Page<DeviceVO>();
        DeviceEntity entity = new DeviceEntity();
        BeanUtils.copyProperties(dto,entity);
        List<DeviceVO> result =deviceMapper.queryPageList(entity);
        return page.setRecords(result);
    }


    @Override
    public List<DeviceVO> queryListByCondition(DeviceDTO dto) {
        DeviceEntity entity = new DeviceEntity();
        BeanUtils.copyProperties(dto,entity);
        List<DeviceVO> list =deviceMapper.queryPageList(entity);
        return list;
    }

    private DeviceEntity setDefaultValue(DeviceDTO dto){
        DeviceEntity entity = new DeviceEntity();
        Integer employeeId=LoginDetailUtils.getCurrentEmployeeId();
        if(StringUtils.isEmpty(dto.getSn())){
            dto.setSn(dto.getImei());   //手机若没SN默认取IMEI
        }
        if(dto.getBelongId()==0){
            dto.setBelongId(DeviceEunm.COMPANY_BELONG.getCode());//默认归属单位
        }
        if(dto.getTypeId()==0){
            dto.setTypeId(DeviceEunm.PDA.getCode());//默认PDA
        }
        if(dto.getPlatformId()==0){
            dto.setPlatformId(DeviceEunm.ANDROID.getCode());//默认安卓
        }
        if(dto.getIsOrganization()==0&&dto.getIsUser()==0){
            dto.setIsOrganization(DeviceEunm.ORG_BIND.getCode()); //默认绑定机构
        }
        if(DeviceEunm.DEVICE_CREATE.getCode().equals(dto.getIsResgister())){ //设备登记
            dto.setIsAction(DeviceStatusEunm.DEVICE__OFF.getCode());
        }
        if(DeviceEunm.DEVICE_REGISTER.getCode().equals(dto.getIsResgister())){ //设备注册
            dto.setIsAction(DeviceStatusEunm.DEVICE_ON.getCode());
            //1.设备命令表


        }
        dto.setCreateBy(employeeId);
        dto.setCreateTime(new Date());
        dto.setRdStatus(DeviceStatusEunm.NORMAL.getCode());
        BeanUtils.copyProperties(dto,entity);
        return entity;
    }

    @Override
    public List<DeviceVO> queryModelByTypeId(Integer typeId) {

        return deviceMapper.queryModelByTypeId(typeId);
    }
}
